import { defineStore } from "pinia";
import { reqLogin } from "@/api/user";
import type {
  loginForm,
  loginResponseData,
  userResponseData,
} from "@/api/user/type";

const useUserStore = defineStore("User", {
  state: () => ({
    token: localStorage.getItem("TOKEN") || "",
    menuRoutes: [],
    buttons: [],
  }),
  getters: {},
  actions: {
    async userLogin(data: loginForm) {
      console.log(data);
      const result = await reqLogin(data);
      // 登录成功 200 -> 存储token
      if (result.code === 200) {
        this.token = result.data.token;
        localStorage.setItem("TOKEN", result.data.token);
      } else {
        // return Promise.reject(new Error(result.data.message))
      }
      console.log(result);
    },
    // setToken(token: string) {
    //   this.token = token
    // },
    // setMenuRoutes(menuRoutes: any[]) {
    //   this.menuRoutes = menuRoutes
    // },
    // setButtons(buttons: string[]) {
    //   this.buttons = buttons
    // },
  },
});

export default useUserStore;
